<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: nonant <nonant@thinksns.com>
// +----------------------------------------------------------------------
//

class ViewAction extends BaseAction{
	
	//空间首页
	function index(){
		$uid = $spaceInfo['uid'];
		$pVisitor = D('Visitor');
		
		if($this->mid!=$this->visitUid){
			
			$map['uid']     = $this->visitUid;
			$map['visitor'] = $this->mid;
			$info=D('Visitor')->where($map)->field('id')->find();
			if(!$info){
				$map['cTime']   = time();
				$pVisitor->data($map)->add();
			}else{
				$map['cTime']   = time();
				$pVisitor->where('id='.$info['id'])->data($map)->save();
			}
		}else{
			if(!isset($_GET['preview'])) redirect(U('home/User/index',array('uid'=>$this->mid)));
		}

		$space            = D('Space','home')->get_Space($this->visitUid);
		$space['info']    = $spaceInfo ;
 		$space['visitors'] = D('Visitor')->where('uid='.$this->visitUid)->limit(12)->field("visitor")->order('cTime DESC')->findall();
		$space['feeds']   = X('Feed')->get(array('uid'=>$this->visitUid));
		$space['walls']   = D('Wall')->getWall($this->visitUid,8);
		$space['record']  = D('Record','home')->getNewData($this->visitUid);
		$space['followMe'] = $this->_followMe();
		$space['followYou'] = $this->_followYou();		
		//dump($space['record']);
		//dump($space['walls']);
		$this->assign('space',$space);
		D('Space')->setInc('visitnum','uid='.$this->visitUid,1);
		//关注统计
		$dao = service('Follow');
		$this->assign('FollowedCount', $dao->getDataCount($this->visitUid,'user'));
		$this->assign('FollowCount', $dao->getUserCount($this->visitUid,'user'));
		$sopt['title'] = $space['baseinfo']['username']['value'].L('action_0001');
		$this->setTitle($sopt);
		
		$list = M('UserCredit')->where("uid=8 and action='ask_weekly_user'")->field('mtime')->findall();
		foreach ($list as $v){
			$arr[] = date('Y-m-d H:i:s', $v['mtime']);
		}
		//dump($arr);
		
		$this->display();
	}

	//个人详细资料
	function profile(){
		$list = D('Space','home')->get_data($this->uid);
		$this->assign('list',$list);
		
		$sopt['title'] = getUserName($this->uid).L('action_0002');
		$this->setTitle($sopt);
		$this->display();
	}

	//动态
	function feed(){
		$time = getdate(time());
		if($_GET['d']=='today'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday'],$time['year']));
		}elseif ($_GET['d']=='weekday'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday']-7,$time['year']));
		}elseif ($_GET['d']=='month'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],1,$time['year']));
		}
		$map['uid'] = $this->visitUid;
		$space['feeds']            = X('Feed')->get($map,10,true,0,false,false);
		$this->assign('space',$space);
		$this->assign('type',$strType);
		$this->assign('d',$_GET['d']);
		$sopt['title'] = getUserName($this->uid).L('action_0003');
		$this->setTitle($sopt);		
		$this->display();
	}
	
	//留言
	function wall(){
		$dao = D('Wall');
		$time = getdate(time());
		if($_GET['d']=='today'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday'],$time['year']));
		}elseif ($_GET['d']=='weekday'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday']-7,$time['year']));
		}elseif ($_GET['d']=='month'){
			$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],1,$time['year']));
		}
		$map['uid'] = $this->visitUid;
		$map['fid'] = 0;
		$list = $dao->where($map)->order('id DESC')->findpage(10);
		foreach ($list['data'] as $key=>$val){
			if(0!=$dao->where("fid=$val[id]")->count()){
				$list['data'][$key]['reply'] = $dao->where("fid=$val[id]")->order('id ASC')->findall();
			}
		}
		$this->assign('d',$_GET['d']);
		$this->assign($list);
		$sopt['title'] = getUserName($this->uid).L('action_0004');
		$this->setTitle($sopt);		
		$this->display();
	}

	//记录
	function record(){
		if($_GET['rid']){
			$map['id'] = intval($_GET['rid']);
			$map['uid'] = $this->mid;
			$list = D('Record','home')->getData($map,1);
			$list = $list['data']['0'];
			if($list['id']){
				$this->assign('record',$list);
			}else{
				$this->error(L('action_0005'));
			}
		}else{
			$time = getdate(time());
			$_GET['d'] = in_array($_GET['d'],array('today','weekday','month'))?$_GET['d']:'';
			if($_GET['d']=='today'){
				$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday'],$time['year']));
			}elseif ($_GET['d']=='weekday'){
				$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],$time['mday']-7,$time['year']));
			}elseif ($_GET['d']=='month'){
				$map['cTime'] = array('gt',mktime(0,0,0,$time['mon'],1,$time['year']));
			}
			
			$map['uid'] = $this->visitUid;
			$list = D('Record','home')->getData($map,7);
			
			//转换url [杨德升添加]
			foreach($list['data'] as $k => $v) {
				$list['data'][$k]['content'] = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $list['data'][$k]['content']);
			}

			$this->assign('list',$list['data']);
			$this->assign('page',$list['html']);
			$this->assign('d',$_GET['d']);
		}

		$sopt['title'] = getUserName($this->uid).L('action_0006');
		$this->setTitle($sopt);
		$sopt['title'] = getUserName($this->uid).L('action_0007');
		$this->setTitle($sopt);		
		$this->display();
	}

	function track(){

		$map['uid'] = $this->uid;
		$list = M('space_visitor')->where($map)->order('cTime DESC')->findpage(20);
		$this->assign('type',$type);
		$this->assign($list);
		$sopt['title'] = getUserName($this->uid).L('action_0008');
		$this->setTitle($sopt);		
		$this->display();
	}
	
	//收藏
	function favorite(){
		$sopt['title'] = getUserName($this->uid).L('action_0009');
		$this->setTitle($sopt);		
		$this->display();
	}

	//关注
	function addFollowUser(){
		$dao = service('Follow');
		$res = $dao->addFollowData($this->visitUid,'user');//dump($res);dump($dao->getLastSql());
		if($res){
			$title['followuid']   = $this->visitUid;
			$title['followuname'] = getUserName($this->visitUid);
			x('Feed')->put('space_follow',$title);
			x('Notify')->put('space_follow',$this->visitUid);
			echo 1;
		}else{
			echo 0;
		}
	}

	function delFollowUser(){
		$dao = service('Follow');

		$res = $dao->delFollowData($this->visitUid,'user');
		if($res){
			echo 1;
		}else{
			echo 0;
		}
	}

	function isFollow(){
		if($this->mid==$this->visitUid){
			echo -1;exit;
		}

		$dao = service('Follow');

		$res = $dao->isFollow($this->visitUid,'user');
		if($res){
			echo 1;
		}else{
			echo 0;
		}
	}
	//他关注的人
	function followYou(){
		$dao = service('Follow');

		$map['uid'] = $this->visitUid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'aimId',55);
		foreach($list['data'] as &$v){
			$v = $v['aimId'];
		}
		$this->assign($list);
		$sopt['title'] = L('action_0011').getUserName($this->uid).L('action_0010');
		$this->setTitle($sopt);
		$this->display();
	}

	//关注他的人
	function followMe(){
		$dao = service('Follow');

		$map['aimId'] = $this->visitUid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'uid',55);
		foreach($list['data'] as &$v){
			$v = $v['uid'];
		}
		$this->assign($list);
		$sopt['title'] = getUserName($this->uid).L('action_0012');
		$this->setTitle($sopt);
		$this->display();
	}
	
	//他的分享
	function share(){
		$map['isDel'] = 0;
		$map['uid']   = $this->visitUid;
		$map['type']  = array('NEQ',2);

		$result = D('Share','home')->getShareList($map);

		$this->assign('data',$result['data']);
		$this->assign('html',$result['html']);
		$this->assign('count',$result['count']);
		$sopt['title'] = getUserName($this->uid).L('action_0013');
		$this->setTitle($sopt);
		$this->display();
	}
	
	//他的论坛
	function bbs(){
		$data = D('Topic','bbs')->where('uid='.$this->visitUid)->order('cTime DESC')->findpage(30);
		$this->assign( $data ); 
		$this->assign('type',$type);	
		$this->display();
	}
	
	//他的团队
	function group(){
		$group = D('Group','group')->getGroup( $this->visitUid,'show' );
		$this->assign( $group );
		$this->display();
	}

	//他的百科
	function wiki() {
		$wiki = M('wiki')->where('authorId='.$this->visitUid)->order('id DESC')->findPage(30);
		$this->assign($wiki);
		$this->display();
	}
	
	//我关注的人-右边菜单栏
	function _followYou(){
		$dao = service('Follow');
		
		$map['uid'] = $this->visitUid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'aimId',0);
		foreach($list as $k=>&$v){
			if($k>8) unset($list[$k]);
			$v = $v['aimId'];
		}
		
		return $list;
	}
	
	//关注我的人-右边菜单栏
	function _followMe(){
		$dao = service('Follow');
		
		$map['aimId'] = $this->visitUid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'uid',0);
		foreach($list as $k=>&$v){
			if($k>8) unset($list[$k]);
			$v = $v['uid'];
		}	
			
		return $list;		
	}		
}
?>